package cn.com.czpb.client.app;

import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.gyf.barlibrary.ImmersionBar;
import com.umeng.analytics.MobclickAgent;

import cn.com.czpb.client.R;
import cn.com.czpb.client.model.event.DayNightEvent;
import cn.com.czpb.client.services.NetWorkChangeReceiver;
import cn.com.czpb.client.utils.SPUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import de.greenrobot.event.Subscribe;


public abstract class MToolBarActivity extends BaseActivity {
    private InputMethodManager imm;
    protected ImmersionBar mImmersionBar;
    private Unbinder unbinder;
    App app;
    protected SPUtil spu;//
    protected Activity activity;
    protected FragmentManager fm;
    Boolean isNight;
    private IntentFilter intentFilter;
    private NetWorkChangeReceiver netWorkChangeReceiver;
    private LocalBroadcastManager manager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme_NoActionBar);
        setContentView(setLayoutId());

        activity = this;
        fm = getSupportFragmentManager();
        spu=SPUtil.getInstance();
        isNight = spu.getIsNight();
        //获取当前的屏幕亮度并设置
        spu.changeAppBrightness(activity, isNight);
        app = (App) getApplication();
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        initNetWork();
        //绑定控件
        unbinder = ButterKnife.bind(this);


        //初始化沉浸式
        if (isImmersionBarEnabled())
            initImmersionBar();
        //初始化数据
        //view与数据绑定
        initView();
        initData();

        //设置监听
        setListener();
    }

    //网络监听广播
    private void initNetWork() {

        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netWorkChangeReceiver = new NetWorkChangeReceiver();
        activity.registerReceiver(netWorkChangeReceiver, intentFilter);
    }
    @Subscribe
    public void onEventMainThread(DayNightEvent event) {
        boolean mFlagNight = event.ismFlagSelectNight();
        spu.changeAppBrightness(this, spu.getIsNight());
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        activity.unregisterReceiver(netWorkChangeReceiver);
        this.imm = null;
        if (mImmersionBar != null)
            mImmersionBar.destroy();  //在BaseActivity里销毁
    }

    protected abstract int setLayoutId();

    protected void initImmersionBar() {
        //在BaseActivity里初始化
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarDarkFont(true);
        mImmersionBar.init();
    }

    protected void initData() {
    }

    protected void initView() {
    }

    protected void setListener() {
    }

    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    public void finish() {
        super.finish();
        hideSoftKeyBoard();
    }
    public void hideSoftKeyBoard() {
        View localView = getCurrentFocus();
        if (this.imm == null) {
            this.imm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        }
        if ((localView != null) && (this.imm != null)) {
            this.imm.hideSoftInputFromWindow(localView.getWindowToken(), 2);
        }
    }
//    @Override
//    protected void onStop() {
//        if (!Anti_hijackingUtils.checkActivity(this)){
//            TUtils.toast("广西云已进入后台运行");
//        }
//        super.onStop();
//    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
